package com.keyancompany.zhongbaopingtai.activity;

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.ViewPagerAdapter;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;

/**
 * @{# GuideActivity.java Create on 2017-5-2 下午10:59:08
 * <p>
 * class desc: 引导界面
 * <p>
 * <p>
 * Copyright: Copyright(c) 2013
 * </p>
 * @Version 1.0
 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
 */
public class GuideActivity extends Activity implements OnPageChangeListener {

    private ViewPager vp;
    private ViewPagerAdapter vpAdapter;
    private List<View> views;

    private int[] guides = {R.mipmap.bg_guide1, R.mipmap.bg_guide2, R.mipmap.bg_guide3, R.mipmap.bg_guide4, R.mipmap.bg_guide5};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        // 初始化页面
        initViews();

        // 初始化底部小点
    }

    private void initViews() {

        views = new ArrayList<View>();
        // 初始化引导图片列表
        for (int i = 0; i < guides.length; i++) {
            int id = guides[i];
            View view = View.inflate(this, R.layout.item_guide, null);
            view.setBackgroundResource(id);
            ImageView startIcon = (ImageView) view.findViewById(R.id.start);
            startIcon.setVisibility(View.GONE);
            if (i == guides.length - 1) {
                startIcon.setVisibility(View.VISIBLE);
                startIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setGuided();
                        goHome();
                    }
                });
            }
            views.add(view);
        }

        // 初始化Adapter
        vpAdapter = new ViewPagerAdapter(views, this);

        vp = (ViewPager) findViewById(R.id.viewpager);

        vp.setAdapter(vpAdapter);
        // vp.setAdapter( new MyPagerAdapter());
        // 绑定回调
        vp.setOnPageChangeListener(this);
        // 绑定回调
    }


    int i = 1;


    // 当当前页面被滑动时调用
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }


    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void goHome() {
        // 跳转
        Intent intent = new Intent(this, LoginActivity.class);
        this.startActivity(intent);
        this.finish();
    }

    /**
     * method desc：设置已经引导过了，下次启动不用再次引导
     */
    private void setGuided() {
        // 存入数据
        SPUtils.putCommData(this, "isFirstIn", false);
    }
}
